package com.designschool.base.http;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;


import android.R.bool;
import android.util.Log;

/****
 * 
 * @User   :shibaotong
 * @创建时间 :2013-10-27
 * @描述    :网络请求参数集
 */
public class HttpRequestParams {
	/***
	 * 请求参数以Map的形式
	 */
	private Map<String, String> httpMaps;
	
	public HttpRequestParams()
	{
		httpMaps=new HashMap<String, String>();
	}
	/***
	 * 写进参数
	 * @param key 参数名
	 * @param value 参数值
	 */
	public void put(String key,String value)
	{ 
		if(IsNotEmpty(key)&&IsNotEmpty(value))
			httpMaps.put(key, value);
	}

	/***
	 * 判断空
	 */
	private boolean IsNotEmpty(String content)
	{
		if(content!=null)
			return true;
		return false;
	}
	
	/***
	 * 获取参数
	 */
	private  List<BasicNameValuePair> getParamsList() {
        List<BasicNameValuePair> lparams = new LinkedList<BasicNameValuePair>();

        for(Map.Entry<String, String> entry : httpMaps.entrySet()) {
        	Log.e(entry.getKey()+"===>", entry.getValue());
            lparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        return lparams;
    }
	
	/***
	 * 将获取的参数格式成为字符串，并格式化请求参数编码
	 */
	 protected String getParamString(String encoding) {
	        return URLEncodedUtils.format(getParamsList(), encoding);
	 }
	 
	 /***
	  * 编码化请求参数
	  * @param encoding
	  * @return
	  */
	 public HttpEntity getEntity(String encoding) {
	        HttpEntity entity = null; 
	        
	            try {
	                entity = new UrlEncodedFormEntity(getParamsList(), encoding);
	            } catch (UnsupportedEncodingException e) {
	                e.printStackTrace();
	            } 
	        return entity;
	    }
}
